import {questionData} from '@/static/js/quickly/tcm/qusetionnaireDetail.js';
import { getUrlParam} from '@/static/js/filter.js';
export default{
	data() {
		return {
			currentIndex: 0,
			questionData:JSON.parse(JSON.stringify(questionData)),
			nowType:'平和质',
			currentPageNo: 1,  //设置分页
			questionArr:[], //tab标题
			sex: '',
			id: '',
			current: '',
			mark: '',
			editId:'',
		}
	},
	onLoad() {
		const _this = this;
		_this.current = getUrlParam('current');
		_this.id = getUrlParam('id');
		_this.sex = getUrlParam('sex');
		_this.editId = getUrlParam('editId');
		_this.current = getUrlParam('current');
		_this.currentIndex = _this.current || 0;
		if(_this.current){
			if(_this.editId && !localStorage.getItem('questionData')) {
				_this.switchClick(_this.questionData[_this.current].name,_this.current)
			}
		}
		
		this.sex == 2 ? this.questionData[5].data.splice(6,1): this.questionData[5].data.splice(5,1);
		if(_this.editId && !localStorage.getItem('questionData') && !getUrlParam('readstatus')) {
			_this.$api.findByConstitutionId(_this.editId).then((res) => {
				_this.mark = res.data.result && res.data.result.appConstitutionEntityVo.mark;
				_this.questionData.map((d,di)=>{
					res.data.result[d.name].map((a,ai)=>{
						d.data[ai] && d.data[ai].options.map((b,bi)=>{
							if(!a.healthParame){
								if(a.userOption == b.userOption){
									b.id = a.id
									b.checked = true
									b.checked2 = true
								}
							}else{
								if(b.userOption == b.healthOption){
									if(a.userOption == b.userOption){
										b.id = a.id
										b.checked = true
										b.checked2 = true
									}
								}else{
									if(a.userOption == b.userOption){
										b.id = a.id
										b.checked = true
									}
									if(a.healthOption == b.userOption){
										b.id = a.id
										b.checked2 = true
									}
								}
							}
						})
					})
				})
				
			})
		} else if(localStorage.getItem('questionData') && !getUrlParam('readstatus')) {
			// 从详情页点击回来查看选中的详情
			if(_this.editId ) {
				const questionHistory = localStorage.getItem('questionData') && JSON.parse(localStorage.getItem('questionData'));
				_this.questionData = questionHistory;
				_this.questionArr = _this.questionData[_this.currentIndex].data;
			} else {
				_this.questionArr = _this.questionData[0].data;
				_this.questionArr = _this.questionData.filter(a=>{return a.name == _this.nowType})[0].data;
			}
		}else if(getUrlParam('readstatus')) {
			const id = getUrlParam('editId')
			this.$api.findByConstitutionId(id).then((res) => {
				this.questionData.map((a,index)=>{
					res.data.result[a.name].map((b,bindex)=>{
						a.data[bindex] && a.data[bindex].options.map((c)=>{
							c.checked = false
							c.checked2 = false
							c.id = b.id
							if(c.userOption == b.userOption){
								c.checked = true
								c.checked2 = true
							}
							if(b.healthOption == c.userOption){
								c.checked2 = true
							}
						})
					})
				})
			})
		}
	},
	mounted(){		
		if(!this.current) {
			this.questionArr = this.questionData[0].data;			
		} else {
			this.sex == 2 ? this.questionData[5].data.splice(6,1): this.questionData[5].data.splice(5,1);
			this.questionArr = this.questionData[this.currentIndex].data;
			
			// this.questionArr = this.questionData.filter(a=>{return a.name == this.nowType})[0].data;
		}
	},
	methods: {
		// tab切换
		switchClick(name,index){
			const _this = this;
			uni.pageScrollTo({
				scrollTop: 0,
				duration: 0
			});
			this.currentPageNo = 1;
			this.nowType = name;
			this.currentIndex = index;
			this.questionArr = this.questionData.filter(a=>{return a.name == name})[0].data
			if(localStorage.questionData){
				return;
			}
			this.$api.findByConstitutionId(this.editId).then((res) => {
				const mData = _this.questionData.filter((b,bi)=>{
					return b.name == this.nowType
				})
				res.data.result[this.nowType].map((a,ai)=>{
					mData[0].data[ai] && mData[0].data[ai].options.map((b,bi)=>{
						b.checked = false
						b.checked2 = false
						if(!a.healthParame){
							if(a.userOption == b.userOption){
								b.id = a.id
								b.checked = true
								b.checked2 = true
							}
						}else{
							if(b.userOption == b.healthOption){
								if(a.userOption == b.userOption){
									b.id = a.id
									b.checked = true
									b.checked2 = true
								}
							}else{
								if(a.userOption == b.userOption){
									b.id = a.id
									b.checked = true
								}
								if(a.healthOption == b.userOption){
									b.id = a.id
									b.checked2 = true
								}
							}
						}
					})
				})
			})
		},
		// 判定跳转
		determine() {
			localStorage.setItem('questionData',JSON.stringify(this.questionData))
			if(!getUrlParam('signData')) {
				uni.navigateTo({
					url:'/pages/quickly/tcm/physical?userId='+this.id+'&editId='+this.editId+'&status='+getUrlParam('status')+'&sex='+getUrlParam('sex')
				})
			} else if(getUrlParam('readstatus')){
				uni.navigateTo({
					url:'/pages/quickly/tcm/physical?userId='+this.id+'&editId='+this.editId+'&status='+getUrlParam('status')+'&sex='+getUrlParam('sex')+'&readstatus='+getUrlParam('readstatus')+'&signData='+getUrlParam('signData')
				})
			} else {
				uni.navigateTo({
					url:'/pages/quickly/tcm/physical?userId='+this.id+'&editId='+this.editId+'&status='+getUrlParam('status')+'&sex='+getUrlParam('sex')+'&readstatus='+'&signData='+getUrlParam('signData')
				})
			}
		},
		showToast(filterResult){
			if(filterResult.length > 0){
				if(filterResult[this.i]){
					if(filterResult[this.i].bresult.length > 0){
						uni.showToast({title:'请选择'+filterResult[this.i].name+'底下的'+filterResult[this.i].bresult[0].title,icon:'none'})
					}else{
						this.i++
						this.showToast(this.filterResult);
					}
				}else{
					uni.navigateTo({
						url:'/pages/quickly/tcm/TCM/physical?userId='+this.id+'&editId='+this.editId+'&status='+getUrlParam('status')+'&sex='+getUrlParam('sex')
					})
				}
			}
		},
			// 上一页
		prevPage(){
			uni.pageScrollTo({
				scrollTop: 0,
				duration: 0
			});
			if(this.currentIndex == 0){
				uni.showToast({title:'这已经是第一页了~',icon:'none'})
				return;   
			}
			this.currentIndex--;
			this.questionArr = this.questionData[this.currentIndex].data;
		},
		// 下一页
		nextPage() {
			const _this = this;     
			uni.pageScrollTo({
				 scrollTop: 0,
				 duration: 0
			});
			if(_this.currentIndex == 8) {
				_this.determine();
				return; 
			}
			_this.currentIndex++;          
			_this.questionArr = _this.questionData[_this.currentIndex].data
		},
		// 选答案
		selectedTitle(aitem,item) {
			if(getUrlParam('readstatus') == 1 || getUrlParam('signData')) {	
				return;
			}
			const checkIndex = item.options.findIndex((a)=>{return a.checked})
			if(!aitem.checked2){
				item.options.map((a)=>{
					a.checked2 = false
					a.id = item.options[checkIndex].id
				})
				aitem.checked2 = true
				aitem.checked = false
			}
			localStorage.setItem('questionData',JSON.stringify(this.questionData)) 
		}
	}
}